//
//  AddVideoView.m
//  VideoPlayer
//
//  Created by 白璐 on 16/9/19.
//  Copyright © 2016年 baidu. All rights reserved.
//

#import "AddVideoView.h"

@interface AddVideoView () <UITextFieldDelegate, UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *closeButton;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UILabel *urlHolder;
@property (weak, nonatomic) IBOutlet UITextView *urlField;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layout;
@end

@implementation AddVideoView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowRadius = 3.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    
    // add keyboard notifications
    NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(onKeyboardShow:)
               name:UIKeyboardWillShowNotification
             object:nil];
    [nc addObserver:self
           selector:@selector(onkeyboardHide:)
               name:UIKeyboardWillHideNotification
             object:nil];
    
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)closeKeyboard {
    [self.nameField resignFirstResponder];
    [self.urlField resignFirstResponder];
}

- (IBAction)onConfirmAdd:(id)sender {
    NSUInteger nameLength = self.nameField.text.length;
    NSUInteger urlLength = self.urlField.text.length;
    
    if (nameLength == 0 || urlLength == 0) {
        return;
    }
    
    if (self.addVideoBlock) {
        self.addVideoBlock(self.nameField.text, self.urlField.text);
    }
    
    [self closeSelf];
}

- (IBAction)onClose:(id)sender {
    [self closeSelf];
}

- (void)closeSelf {
    [self removeFromSuperview];
    self.nameField.text = @"";
    self.urlField.text = @"";
    self.urlHolder.hidden = NO;
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    self.urlHolder.hidden = YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    self.urlHolder.hidden = (textView.text.length != 0);
}

- (void)onKeyboardShow:(NSNotification*)notification {
    self.layout.constant = -100;
}

- (void)onkeyboardHide:(NSNotification*)notification {
    self.layout.constant = 0;
}

@end
